﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SQLMAPApp
{

    /// <summary>
    /// 
    /// </summary>
    public class sameple_article
    {

        /// <summary>
        /// <![CDATA[新增一条数据]]>
        /// </summary>
        /// <returns></returns>
        public int Insert()
        {
            //dto
            SQLMap.Dto.sampe_article article = new SQLMap.Dto.sampe_article()
            {
                title = "法国部署十万军警",
                keywords = "法国",
                body = "法国部署十万军警",
                createBy = 1,
                modifyBy = 1,
                createOn = DateTime.Now,
                modifyOn = DateTime.Now
            };
            int iResult = DbHelper.ExcuteNonQuery(sqlId: "insert1", parameters: article);
            return iResult;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public DataSet Query()
        {
            //CommandInfor
            System.Data.DataSet ds = DbHelper.Query("select1");

            //循环Row
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                Console.WriteLine(string.Format("{0} {1}", ds.Tables[0].Rows[i][0], ds.Tables[0].Rows[i][1]));
            }
            return ds;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public DataSet Search()
        {
            //CommandInfor
            System.Data.DataSet ds = DbHelper.Query("select2", new { id = 2, title = "法国" });
            return ds;
        }

        /// <summary>
        /// 删除操作
        /// </summary>
        /// <returns></returns>
        public int Delete()
        {
            //CommandInfor
            int iResult = DbHelper.ExcuteNonQuery("delete1", new { id = 1 });
            return iResult;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public int Update(SQLMap.Dto.sampe_article article)
        {
            int iResult = DbHelper.ExcuteNonQuery("update1", article);
            return iResult;
        }


        /// <summary>
        /// <![CDATA[存储过程：根据文章id获取文章实体]]>
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public SQLMap.Dto.sampe_article GetArticle(int id)
        {
            SQLMap.Dto.sampe_article model = null;
            using (IDataReader data = DbHelper.ExecuteDataReader("sp_getArticleId", new { id = id }))
            {
                if (data.Read())
                {
                    model = new SQLMap.Dto.sampe_article();
                    model.id = data.GetInt32(0);
                    model.title = data.GetString(1);
                    model.keywords = data.GetString(2);
                    model.body = data.GetString(3);
                }
            }
            return model;
        }
    }

}
